/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.wicket.behaviors;

import org.apache.wicket.Component;
import org.apache.wicket.behavior.AbstractBehavior;
import org.apache.wicket.markup.html.IHeaderResponse;

/**
 * Prevents a form from submitting if the user presses the 
 * enter key in an input field. Can be used for Ajax and 
 * non Ajax forms.
 * 
 * Requires jQuery to work
 * @author jeff
 */
public class PreventFormSubmitOnEnterBehavior extends AbstractBehavior {

    private Component component = null;
    
    @Override
    public void bind(Component component) {
        super.bind(component);
        this.component = component;
    }

    @Override
    public void renderHead(IHeaderResponse response) {
        super.renderHead(response);
        response.renderJavascript("$(document).ready(function(){$('#" + component.getMarkupId() + "')" + 
                ".bind('keypress', function(e){var c = e.which ? e.which : e.keyCode;if (c == 13) {return false;}});});", null);
    }
    
}
